package cn.itheima.service.impl;

import cn.itheima.dao.CourierRepositroy;
import cn.itheima.dao.FixedAreaRepository;
import cn.itheima.dao.TakeTimeRepository;
import cn.itheima.domain.Courier;
import cn.itheima.domain.FixedArea;
import cn.itheima.domain.TakeTime;
import cn.itheima.service.CourierService;
import cn.itheima.service.FixedAreaService;
import cn.itheima.service.TakeTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by millions on 2017/7/3.
 */
@Service
@Transactional
public class FixedAreaServiceImpl implements FixedAreaService {

    @Autowired
    private FixedAreaRepository fixedAreaRepository;

    @Autowired
    private CourierRepositroy courierRepositroy;

    @Autowired
    private TakeTimeRepository takeTimeRepository;

    @Override
    public void save(FixedArea model) {
        fixedAreaRepository.save(model);
    }

    @Override
    public Page<FixedArea> pageQuery(Specification<FixedArea> specification, Pageable pageable) {

        return fixedAreaRepository.findAll(specification,pageable);
    }

    /**
     * 更新快递员，区域，收派时间 之间的关联
     * @param courierId
     * @param takeTimeId
     * @param id
     */
    @Override
    public void updateAssociationCourierToFixedArea(Integer courierId, Integer takeTimeId, String id) {

        FixedArea fixedArea = fixedAreaRepository.findOne(id);
        Courier courier = courierRepositroy.findOne(courierId);
        TakeTime takeTime = takeTimeRepository.findOne(takeTimeId);

        fixedArea.getCouriers().add(courier);

        courier.setTakeTime(takeTime);

    }
}
